<?php
class Exam_Form_Exam_Create extends Engine_Form
{
  public function init()
  {
    $user_level = Engine_Api::_()->user()->getViewer()->level_id;
    $user = Engine_Api::_()->user()->getViewer();

    // Init form
    $this
      ->setTitle('Add New Eaxm')
      ->setDescription('Fill infomation into fields bellow')
      ->setAttrib('id', 'form-exam-create')
      ->setAttrib('name', 'exams_create')
      ->setAttrib('enctype','multipart/form-data')
      ->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble(array()))
      ;
    

    // Title
    $this->addElement('Text', 'title', array(
      'label' => 'Exam Title',
      'allowEmpty' => false,
      'required' => true,
      'validators' => array(
        array('NotEmpty', true),
        array('StringLength', false, array(1, 300)),
      ),
      'filters' => array(
        'StripTags',
        new Engine_Filter_Censor(),
      ),
      'style'=>'width:350px;'
    ));

    // prepare categories
    //$categories = Engine_Api::_()->getDbtable('categories', 'exams')->getCategoriesAssoc();
    $categories =  array('1' => 'English', '2' => 'IQ');
    if( count($categories) > 0 ) {
      $this->addElement('Select', 'category_id', array(
        'label' => 'Category',
        'multiOptions' => $categories,
      ));
    }

    // Init descriptions
    $this->addElement('TinyMce', 'description', array(
      'label' => 'Exam Description',
      'required' => false,
      'allowEmpty' => true,
      'filters' => array(
        new Engine_Filter_Html(array('AllowedTags'=>true)),
        new Engine_Filter_Censor(),
      ),
    ));
       
    // Photo
    $this->addElement('File', 'photo', array(
      'label' => 'Main Photo'
    ));
    $this->photo->addValidator('Extension', false, 'jpg,png,gif,jpeg');
    
    //is_forever
    $this->addElement('Radio', 'is_forever', array(
    	'label' => 'Time Period',
    	'multiOptions' => array(
    		'0' => 'Unlimited',
    		'1' => 'Limited'
    	),
    	'value' => '0',
    	'onChange' => 'changeTimeLimit(this);'
    ));
    // Start time
    $start = new Engine_Form_Element_CalendarDateTime('starttime');
    $start->setLabel("Start Time");
    $start->setAllowEmpty(true);
    $this->addElement($start);

    // End time
    $end = new Engine_Form_Element_CalendarDateTime('endtime');
    $end->setLabel("End Time");
    $end->setAllowEmpty(true);
    $this->addElement($end);
    
    //limited or unlimited duration
    $this->addElement('Radio', 'is_limited', array(
    	'label' => 'Duration',
    	'multiOptions' => array(
    		'0' => 'Unlimited',
    		'1' => 'Limited'
    	),
    	'value' => '0',
    	'onChange' => 'changeDuration(this);'
    ));
    
   //init duration
   $this->addElement('text', 'duration', array (
         'label' => '',
       	 'description'	=>	'minutes',
         'allowEmpty' => true,
         'required' => false,
          'style'=>'width:100px;'
        ));
   	$this->duration->addValidator('Int');
	$this->duration->getDecorator('Description')->setOption('style', 'float:right; margin-top:7px;margin-left: 5px;');
    
    //attempts_allowed
    $this->addElement('Checkbox', 'attempts_allowed', array(
      'label' => 'People can attempts allowed',
      'value' => True
    ));
    
    //question_shuffle
    $this->addElement('Checkbox', 'question_shuffle', array(
      'label' => 'Question shuffle',
      'value' => True
    ));
    
    //view_correct
    $this->addElement('Checkbox', 'view_correct', array(
      'label' => 'People can view correct after they tested on this exam',
      'value' => True
    ));
    
    //view_explanation
    $this->addElement('Checkbox', 'view_explanation', array(
      'label' => 'People can view explanation after they tested on this exam',
      'value' => True
    ));
    
    
    
    //ADD AUTH STUFF HERE

    $availableLabels = array(
      'everyone'              => 'Everyone',
      'registered'            => 'All Registered Members',
      'owner_network'         => 'Friends and Networks',
      'owner_member_member'   => 'Friends of Friends',
      'owner_member'          => 'Friends Only',
      'owner'                 => 'Just Me'
    );

    // Element: auth_test
    $testOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('exam', $user, 'auth_test');
    $testOptions = array_intersect_key($availableLabels, array_flip($testOptions));

    if( !empty($testOptions) && count($testOptions) >= 1 ) {
      // Make a hidden field
      if(count($testOptions) == 1) {
        $this->addElement('hidden', 'auth_test', array('value' => key($testOptions)));
      // Make select box
      } else {
        $this->addElement('Select', 'auth_test', array(
            'label' => 'Privacy',
            'description' => 'Who may test this exam?',
            'multiOptions' => $testOptions,
            'value' => key($testOptions),
        ));
        $this->auth_test->getDecorator('Description')->setOption('placement', 'append');
      }
    }

    // Element: auth_comment
    $commentOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('exam', $user, 'auth_comment');
    $commentOptions = array_intersect_key($availableLabels, array_flip($commentOptions));

    if( !empty($commentOptions) && count($commentOptions) >= 1 ) {
      // Make a hidden field
      if(count($commentOptions) == 1) {
        $this->addElement('hidden', 'auth_comment', array('value' => key($commentOptions)));
      // Make select box
      } else {
        $this->addElement('Select', 'auth_comment', array(
            'label' => 'Comment Privacy',
            'description' => 'Who may post comments on this exam?',
            'multiOptions' => $commentOptions,
            'value' => key($commentOptions),
        ));
        $this->auth_comment->getDecorator('Description')->setOption('placement', 'append');
      }
    }

    // Buttons
    $this->addElement('Button', 'submit', array(
      'label' => 'Save Changes',
      'type' => 'submit',
      'ignore' => true,
      'decorators' => array(
        'ViewHelper',
      ),
    ));

    $this->addElement('Cancel', 'cancel', array(
      'label' => 'cancel',
      'link' => true,
      'prependText' => ' or ',
      'decorators' => array(
        'ViewHelper',
      ),
    ));

    $this->addDisplayGroup(array('submit', 'cancel'), 'buttons', array(
      'decorators' => array(
        'FormElements',
        'DivDivDivWrapper',
      ),
    ));
  }
}
